package com.huatai.model.utils;

import com.huatai.model.constant.MinioBucket;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: nanming-saas
 * @description: 解压
 * @author: songxiude
 * @create: 2024-06-25 11:26
 **/
public class WebCompressUtil {

	private static final int BUFFER_SIZE = 2048;

	public static List unTar(InputStream in, MinioClient minioClient, String bucketName, String parentDir) {

		List fileNames = new ArrayList();

		try {

			TarArchiveInputStream tarIn = new TarArchiveInputStream(in, BUFFER_SIZE);

			while (tarIn.getNextTarEntry() != null) {

				TarArchiveEntry entry = tarIn.getNextTarEntry();

				if (entry.isFile()) {

					//minioClient.putObject(bucketName, parentDir + entry.getName(), tarIn, entry.getSize(), null, null, null);
					minioClient.putObject(PutObjectArgs.builder()
						.bucket(MinioBucket.MODEL_BUCKET_NAME_PUBLIC)
						.object(parentDir + entry.getName())
						.stream(tarIn, entry.getSize(), -1)
						.build()
					);

					fileNames.add(parentDir + entry.getName());

				}

			}

		} catch (Exception e) {

			e.printStackTrace();

		}

		return fileNames;

	}
}
